package com.cyx.spring.io.tx;

import com.cyx.spring.annotation.Autowired;
import com.cyx.spring.annotation.Component;
import com.cyx.spring.annotation.Transactional;
import com.cyx.spring.io.jdbc.User;
import com.cyx.spring.jdbc.JdbcTemplate;

import java.sql.SQLException;
import java.util.List;

/**
 * UserService
 *
 * @author yongxiang
 * @date 2024/10/25 14:39
 * @desc
 */
@Component
@Transactional
public class UserService {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void deleteAll(){
        try {
            int deleteFromUser = jdbcTemplate.update("delete from user");
            System.out.println(deleteFromUser);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public void insertStudent() throws Exception{
        deleteAll();
        List<User> users1 = jdbcTemplate.queryForObjectList("select * from user", User.class);
        System.out.println(users1);
        System.out.println("---------");
        if (1/0 == 12){
            System.out.println("--");
        }
        jdbcTemplate.update("insert into user(name,age) values(?,?)", "yongxiang1", 18);
        jdbcTemplate.update("insert into user(name,age) values(?,?)", "yongxiang2", 18);
        List<User> users2 = jdbcTemplate.queryForObjectList("select * from user", User.class);
        System.out.println(users2);
    }
}
